from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.serializers import ModelSerializer
from rest_framework.response import Response
from .models import *
from .ser import *


# Create your views here.

# vue+drf实现添加分类
# vue+drf实现添加商品，添加商品时选择分类
# 展示商品列表，根据价格和库存切换排序
class KindView(APIView):
    def post(self, request):
        # 获取所有的字段进行序列化
        ser = KindSer(data=request.data)
        # 判断这些字段
        if ser.is_valid():
            # 保存
            ser.save()
            # 返回
            return Response({'code': 200})
        else:
            return Response({'code': 400})

    def get(self, request):
        # 获取所有的数据
        show = Kind.objects.all()
        # 把所有的数据进行序列化
        ser = KindSer(show, many=True)
        # 返回结果
        return Response({'code': 200, 'list': ser.data})


class GoodsView(APIView):
    def post(self, request):
        # 获取表里所有的字段
        ser = GoodsSer(data=request.data)
        # 判断这些字段符不符合
        if ser.is_valid():
            # 保存
            ser.save()
            # 返回结果
            return Response({'code': 200})
        else:
            return Response({'code': 400})

    def get(self, request):
        # 获取表里边所有的数据
        show = Goods.objects.all()
        # 进行序列化
        ser = GoodsSer(show, many=True)
        # 返回结果
        return Response({'code': 200, 'list': ser.data})


#  ------
# 查询班级及班级下的课程
# class ShowCourse(APIView):
#     def get(self, request):
#         cla = Sclass.objects.all()
#         ser = SclassSer(cla, many=True)
#         return Response({'code': 200, 'list': ser.data})
#
#
# # 注册
# class Reg(APIView):
#     def post(self, request):
#         ser = StuSer(data=request.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'code': 200})
#         else:
#             return Response({'code': 400})

# 作业
# 分页
from django.core.paginator import Paginator
from django.contrib.auth.hashers import check_password, make_password


class UserM(APIView):
    def post(self, request):
        request.data['password'] = make_password(request.data.get('password'))
        ser = UserSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code': 20010})


class ShopM(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Suser.objects.filter(username=username).first()
        if user:
            if check_password(password, user.password):
                return Response({'code': 200, 'userid': user.id})
            else:
                return Response({'code': 20011, 'mes': '密码错误'})
        else:
            return Response({'code': 20012, 'mes': '用户名错误'})

    def get(self, request):
        cpage = request.GET.get('cpage')
        shop = Shop.objects.all()
        p = Paginator(shop, 2)
        tpage = p.num_pages
        shops = p.page(cpage)
        ser = ShopSer(shops, many=True)
        return Response({'code': 200, 'list': ser.data, 'tpage': tpage})


class ComM(APIView):
    def post(self, request):
        ser = ComSer(data=request.data)
        id = request.data.get('shop')
        try:
            if ser.is_valid():
                ser.save()
                shop = Shop.objects.filter(id=id).first()
                shop.comments += 1
                shop.save()
                return Response({'code': 200})
        except:
            return Response({'code': 20015})


# 老师注册接口
class TeacherM(APIView):
    def post(self, request):
        ser = TeacherSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code': 20010})


# 登录接口
class TeacherLogin(APIView):
    # def post(self,request):
    #     username = request.data.get('username')
    #     password = request.data.get('password')
    #     if username and password:
    #         teacher = Teacher.objects.filter(username=username,password=password).first()
    #         if teacher:
    #             return Response({'code': 200, 'tid': teacher.id, 'tname': teacher.username})
    #         else:
    #             return Response({'code': 20010})
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        # 判断 如果符合这两个就登录
        if username and password:
            # 登录
            teacher = Teacher.objects.filter(username=username, password=password).first()
            # 判断如果账密码都符合
            if teacher:
                # 返回200 用户id  用户名
                return Response({'code': 200, 'tid': teacher.id, 'tname': teacher.username})
        else:
            return Response({'code': 10010})


# 展示所有的老师
class Teacher_view(APIView):
    def get(self, request):
        data = Teacher.objects.all()
        ser = TeacherSer(data, many=True)
        return Response({'code': 200, 'list': ser.data})


# 添加展示学生
class StuM(APIView):
    def post(self, request):
        ser = TstuSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code': 20010})

    def get(self, request):
        tid = request.GET.get('tid')
        stu = Tstu.objects.filter(teacher_id=tid).all()
        ser = TstuSer(stu, many=True)
        return Response({'code': 200, 'list': ser.data})

    def put(self, request):
        id = request.GET.get('id')
        Tstu.objects.filter(id=id).update(teacher_id='')
        return Response({"code": 200})

# # 添加展示学生
# class StuV(APIView):
#     def get(self, request):
#         tid = request.GET.get('tid')
#         if tid:
#             stu = Tstu.objects.filter(teacher_id=tid).all()
#         else:
#             # 没绑定的所有学生
#             stu = Tstu.objects.filter(teacher_id__isnull=True).all()
#         ser = TstuSer(stu, many=True)
#         return Response({"code": 200, 'list': ser.data})
#
#     def put(self, request):
#         # 学生id
#         id = request.GET.get('id')
#         tid = request.GET.get('tid')
#         if tid:
#             # 绑定
#             Tstu.objects.filter(id=id).update(teacher_id=tid)
#         else:
#             # 解绑
#             Tstu.objects.filter(id=id).update(teacher_id='')
#         return Response({"code": 200})
